package com.bbu.javaexam.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbu.javaexam.domain.ExamManage;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Alex
 * @version 1.0.0
 * @ClassName ExamManageMapper.java
 * @Description TODO
 */
@Mapper
public interface ExamManageMapper extends BaseMapper<ExamManage> {
    /**
     * @throws
     * @title 不通过分页查询所有考试
     * @description
     * @author Alex
     * @updateTime 2021/12/5 14:37
     */
    @Select("select * from exam_manage ORDER BY examDate DESC")
    List<ExamManage> findAll();

    /**
     * @throws
     * @title 通过分页查询所有考试
     * @description
     * @author Alex
     * @updateTime 2021/12/5 14:37
     */
    @Select("select * from exam_manage ORDER BY examDate DESC")
    IPage<ExamManage> findAll(Page page);

    /**
     * @throws
     * @title 通过考试代码查找指定考试
     * @description
     * @author Alex
     * @updateTime 2021/12/5 14:37
     */
    @Select("select * from exam_manage where examCode = #{examCode}")
    ExamManage findById(Integer examCode);

    /**
     * @throws
     * @title 删除指定场的考试
     * @description
     * @author Alex
     * @updateTime 2021/12/5 14:37
     */
    @Delete("delete from exam_manage where examCode = #{examCode}")
    int delete(Integer examCode);

    /**
     * @throws
     * @title 更新一场考试的考试信息
     * @description
     * @author Alex
     * @updateTime 2021/12/5 14:39
     */
    @Update("update exam_manage set description = #{description},source = #{source},paperId = #{paperId}," +
            "examDate = #{examDate},totalTime = #{totalTime},grade = #{grade},term = #{term}," +
            "major = #{major},institute = #{institute},totalScore = #{totalScore}," +
            "type = #{type},tips = #{tips} where examCode = #{examCode}")
    int update(ExamManage exammanage);

    /**
     * @throws
     * @title 新增一场考试
     * @description
     * @author Alex
     * @updateTime 2021/12/5 14:39
     */
    @Options(useGeneratedKeys = true, keyProperty = "examCode")
    @Insert("insert into exam_manage(description,source,paperId,examDate,totalTime,grade,term,major,institute,totalScore,type,tips)" +
            " values(#{description},#{source},#{paperId},#{examDate},#{totalTime},#{grade},#{term},#{major},#{institute},#{totalScore},#{type},#{tips})")
    int add(ExamManage exammanage);

    /**
     * @throws
     * @title 查询最后一条记录的paperId, 返回给前端达到自增效果
     * @description
     * @author Alex
     * @updateTime 2021/12/5 14:41
     */
    @Select("select paperId from exam_manage order by paperId desc limit 1")
    ExamManage findOnlyPaperId();
}
